<?php
/**
 * This is intended to be run in the "background" from an XmlHttpRequest call 
 */

include('mailview2.php');
$mailview = new Mailview2();

$link = Mailview\imap_open($_SESSION['config']);

$mailboxes = explode(',', $_SESSION['config']['background']);
if (count($mailboxes) === 0)
{
	print 'STOP';
	exit(0);
}

$check = array();
foreach ($mailboxes as $mbox)
{
	$mbox = trim($mbox);
	$info = Mailview\imap_status($link, $_SESSION['ref'] . $mbox, SA_UNSEEN);

	if (isset($info->unseen))
	{
		$check[] = array(
			'mbox' => $mbox,
			'unseen' => $info->unseen,
		);
	}
}

print json_encode($check);
?>
